﻿using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Text;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace VIALUTIONS.InfoPath.FormsHub.InfoPathFormsHubWebPart
{
    [ToolboxItemAttribute(false)]
    public class InfoPathFormsHubWebPart : Microsoft.SharePoint.WebPartPages.WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string AscxPath = @"~/_CONTROLTEMPLATES/InfoPath_Forms_Hub_Web_Part/VisualWebPart1/InfoPathFormsHubWebPartUserControl.ascx";

        #region Properties

        [Personalizable(PersonalizationScope.Shared),
        WebBrowsable(false)]
        public Collection<InfoPathForm> InfoPathForms { get; set; }

        #endregion

        /// <summary>
        /// Add a custom EditorPart to the
        /// Edit Web Part view.
        /// </summary>
        /// <returns>A collection of EditorParts</returns>
        public override EditorPartCollection CreateEditorParts()
        {
            var listEditorParts = new List<EditorPart>();
            var editorPart = new WebPartProperties("FormsHubWebPart" + ClientID, "InfoPath Forms Hub");
            listEditorParts.Add(editorPart);
            return new EditorPartCollection(listEditorParts);
        }

        /// <summary>
        /// Method creates all html controls for
        /// the InfoPath Forms Hub Web Part.
        /// </summary>
        protected override void CreateChildControls()
        {
            var control = Page.LoadControl(AscxPath);
            Controls.Add(control);

            CreateFormsList();
        }        

        /// <summary>
        /// Builds a link list from all
        /// configured InfoPath forms.
        /// </summary>
        private void CreateFormsList()
        {
            if (InfoPathForms == null)
                return;

            foreach (var form in InfoPathForms)
            {
                var link = new StringBuilder();
                link.Append("<a href=\"javascript:openModalDialog('");
                link.Append("New " + form.Name);
                link.Append("', '");
                link.Append(GetFormsServerUrl(form));
                link.Append("');\">");
                link.Append(form.Name);
                link.Append("</a></br>");

                Controls.Add(new LiteralControl(link.ToString()));
            }
        }
        /// <summary>
        /// Method builds a link to the InfoPath
        /// Forms Service that is based on the information
        /// of the selected InfoPath Form.
        /// </summary>
        /// <param name="form">A InfoPath forms configuration</param>
        /// <returns>The link to the InfoPath Forms Service</returns>
        private string GetFormsServerUrl(InfoPathForm form)
        {
            var sUrl = new StringBuilder();
            var site = new SPSite(form.Source);

            sUrl.Append(site.Url);
            sUrl.Append("/_layouts/FormServer.aspx?XsnLocation=");
            sUrl.Append(form.Source);
            sUrl.Append("&SaveLocation=");
            sUrl.Append(!string.IsNullOrEmpty(form.Target) ? form.Target : form.Source.Substring(0, form.Source.LastIndexOf("/")));
            sUrl.Append("&DefaultitemOpen=1");

            return sUrl.ToString();
        }
    }
}
